﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.Silverlight.Library
{

	/// <summary>
	/// Declaration of ReadOnlyBankAccountCtrl class
	/// Display of bank account details
	/// </summary>
	public partial class ReadOnlyBankAccountCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public ReadOnlyBankAccountCtrl()
		{

			// Load xaml resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Event handlers

		private void RequestModificationButton_Click(object sender, EventArgs e)
		{

			// Find parent control
			ReadOnlyContactDetailsCtrl parentCtrl = this.FindParent(typeof(ReadOnlyContactDetailsCtrl)) as ReadOnlyContactDetailsCtrl;

			// Request a modification for this field
			parentCtrl.RequestModification(Definitions.eDataField.Contact_BankAccount, 0, null);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public void Set(BankAccountTransport bankAccount)
		{

			// Bank name
			tbBank.Text = bankAccount.BankName;
			tbAccName.Text = bankAccount.AccountName;
			tbSortCode.Text = bankAccount.SortCode;
			tbAccNo.Text = bankAccount.AccountNo;

			if (string.IsNullOrEmpty(bankAccount.IBANNo))
			{
				tbIbanLabel.Visibility = System.Windows.Visibility.Collapsed;
				tbIban.Visibility = System.Windows.Visibility.Collapsed;
			}
			else
			{
				tbIbanLabel.Visibility = System.Windows.Visibility.Visible;
				tbIban.Visibility = System.Windows.Visibility.Visible;
				tbIban.Text = bankAccount.IBANNo;
			}

			if (string.IsNullOrEmpty(bankAccount.SwiftCode))
			{
				tbSwiftLabel.Visibility = System.Windows.Visibility.Collapsed;
				tbSwift.Visibility = System.Windows.Visibility.Collapsed;
			}
			else
			{
				tbSwiftLabel.Visibility = System.Windows.Visibility.Visible;
				tbSwift.Visibility = System.Windows.Visibility.Visible;
				tbSwift.Text = bankAccount.SwiftCode;
			}

			Money.eCurrency currency = Money.ParseCurrencyCode(bankAccount.CurrencyCode);
			if (currency == Money.eCurrency.NotSet)
				tbCurrency.Text = bankAccount.CurrencyCode;
			else
				tbCurrency.Text = string.Format("{0} ({1})", bankAccount.CurrencyCode, Money.CurrencyToSymbol(currency));

			TransportSupport.AddressToTextBlockContent(bankAccount.Address, tbAddress);

		}

	}

}
